//
//  FQDMySubscriptionViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/19/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMySubscriptionViewController.h"
#import "FQDRegisterNavViewController.h"
#import "FQDSimpleFeedCellItemProvider.h"
#import "FQDAccountManager.h"
#import "FQDSubscribeManager.h"
#import "FQDColumnArticleLargeCell.h"
#import "FQDColumnArticleSmallCell.h"
#import "FQDMySubscriptionEmptyView.h"
#import "UIView+FQDExtension.h"
#import "UIImage+ImageEffects.h"
#import "FQDArticleFeedCellItem.h"
#import "FQDRestModel.h"

static NSString *const kMySubscriptionFeedCacheId = @"my_suscription";

@interface FQDMySubscriptionViewController () <FQDMySubscriptionEmptyViewDelegate>

@property (nonatomic, strong) FQDMySubscriptionEmptyView *emptyView;
@property (nonatomic, assign) BOOL needsRefresh;

@end

@implementation FQDMySubscriptionViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _needsRefresh = YES;
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupCellItemProvider];
    [self setupNotifications];
    [self refreshView];
}

- (void)setupCellItemProvider
{
    FQDSimpleFeedCellItemProvider *provider = [[FQDSimpleFeedCellItemProvider alloc] init];
    provider.cacheId = kMySubscriptionFeedCacheId;
    [provider setFetchFeedBlock:^(NSString *feedId, NSString *cursorKey, QDailyAPICompleteBlock completion) {
        [[QDailyAPI sharedAPI] getColumnSubscriptionPosts:cursorKey completion:completion];
    }];
    self.provider = provider;
}

- (void)setupNotifications
{
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(userAccountDidChangeNotification:)
                          name:FQDAccountUserInfoDidChangeNotification object:nil];
    [defaultCenter addObserver:self selector:@selector(subscribeColumnsChanged:)
                          name:FQDSubscribeColumnsDidChangeNotification object:nil];
}

- (void)refreshView
{
    WeakSelf;
    BOOL isLogined = [[FQDAccountManager sharedManager] isLogined];
    if (isLogined) {
        [self loadNewCellItemsWithCompletion:^(BOOL success) {
            BOOL hasSubscribeColumns = [[FQDSubscribeManager sharedManager] hasSubscribedAnyColumn];
            weakSelf.feedContentView.hidden = !hasSubscribeColumns;
            weakSelf.emptyView.hidden = hasSubscribeColumns;
            if (!hasSubscribeColumns) {
                weakSelf.emptyView.type = FQDMySubscriptionEmptyViewTypeNotSubscribed;
            }
        }];
    } else {
        self.feedContentView.hidden = YES;
        self.emptyView.hidden = NO;
        self.emptyView.type = FQDMySubscriptionEmptyViewTypeNotLogined;
    }
}

- (FQDMySubscriptionEmptyView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[FQDMySubscriptionEmptyView alloc] initWithFrame:self.view.bounds];
        _emptyView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _emptyView.hidden = YES;
        _emptyView.delegate = self;
        [self.view addSubview:_emptyView];
    }
    return _emptyView;
}

#pragma mark - FQDFeedBaseViewController override
- (NSInteger)numberOfHeaderCellsInCollectionView:(UICollectionView *)collectionView
{
    return 0;
}

- (Class)cellClassForCellItem:(FQDFeedCellItem *)cellItem
{
    FQDArticleFeedCellItem *articleCellItem = (FQDArticleFeedCellItem *)cellItem;
    FQDFeed *articleFeed = articleCellItem.feed;
    if (articleFeed.type == FQDFeedTypeLargeArticle) {
        return [FQDColumnArticleLargeCell class];
    } else if (articleFeed.type == FQDFeedTypeRegularArticle) {
        return [FQDColumnArticleSmallCell class];
    }
    return nil;
}

- (NSArray *)cellClasses
{
    return @[
        [FQDColumnArticleLargeCell class],
        [FQDColumnArticleSmallCell class],
    ];
}

#pragma mark - FQDMySubscriptionEmptyViewDelegate
- (void)didSelectActionButtonInEmptyView:(FQDMySubscriptionEmptyView *)emptyView
{
    if (emptyView.type == FQDMySubscriptionEmptyViewTypeNotLogined) {
        FQDRegisterNavViewController *navController = [[FQDRegisterNavViewController alloc] init];
        navController.backgroundViewController = self;
        [self presentViewController:navController animated:YES completion:nil];

    } else if (emptyView.type == FQDMySubscriptionEmptyViewTypeNotSubscribed) {
        if ([self.delegate respondsToSelector:@selector(subscriptionViewController:didClickedMoreColumnsButton:)]) {
            [self.delegate subscriptionViewController:self didClickedMoreColumnsButton:emptyView.actionButton];
        }
    }
}

#pragma mark - Notifications
- (void)userAccountDidChangeNotification:(NSNotification *)note
{
    [self refreshView];
}

- (void)subscribeColumnsChanged:(NSNotification *)note
{
    [self refreshView];
}

@end
